﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;
using BookModel;

namespace WinFromBook
{
    public partial class FrmBorrwer : CCSkinMain
    {
        public FrmBorrwer()
        {
            InitializeComponent();
        }
        #region 封装方法，字段，执行操作

        public FrmBorrowhistory FB { get; set; }
        public string BorrowerID { get; set; }

        //加载事件
        #region 加载借阅记录事件
        public void loadBorrower()
        {
            this.skinLaReturn.Visible = true;
            this.skinComboReturn.Visible = true;
            this.skinLaP1.Visible = true;
            this.skinComMakeup.Visible = true;
            this.skinLabMakeUp.Visible = true;
            this.skinComboMakeUp2.Visible = true;
            this.skinLabreturn.Visible = true;
            this.dateTime2.Visible = true;
            foreach (var item in new BLL.borrowerIfnoBLL().GetBorrowerInfo())
            {
                if (item.BorrowerID == int.Parse(BorrowerID))
                {
                    skinborrowerID.Text = BorrowerID;
                    skinRID.Text = item.ReaderID + "";
                    skinComBookId.Text = item.Bid + "";
                    rtfRichMain.Text = item.Borrowerremark;
                    skinComBorrwer.Text = item.BorrowerSate;
                    skinDataTime.Text = item.BorrowerDate + "";
                }
            }
        }
        #endregion

        #region 下拉框加载
        public void ComBoxLoad()
        {
            foreach (var item in new BLL.BookStateInfoBLL().GetState())
            {
                if (item.BookState == "未借出")
                {
                    this.skinComBookId.Items.Add(item.BID);
                }
            }
            this.skinComBookId.SelectedIndex = 0;
            this.skinComBorrwer.SelectedIndex = 0;
            this.skinComMakeup.SelectedIndex = 0;
            this.skinComboReturn.SelectedIndex = 0;
        }
        #endregion
        #region 下拉框组合事件
        //图书下拉框文本更改事件
        private void skinComBookId_MouseLeave(object sender, EventArgs e)
        {
            if (Getname())
            {
                MessageBox.Show("该图书已借出或不存在", "系统提示");
            }
        }
        //查询图书名称
        public bool Getname()
        {
            if (!String.IsNullOrEmpty(this.skinComBookId.Text))
            {
                int ID = int.Parse(this.skinComBookId.Text);
                foreach (var item in new BLL.BookInfoBLL().BookInfoAll())
                {
                    if (item.BID == ID)
                    {
                        skinBname.Text = item.Bname;
                        return false;
                    }
                }
            }
            return true;
        }
        //关闭下拉框事件
        private void skinComBookId_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            Getname();
        }
        //文本框更改
        private void skinBname_MouseLeave(object sender, EventArgs e)
        {
            string name = this.skinBname.Text.Trim();
            foreach (var item in new BLL.BookInfoBLL().BookInfoAll())
            {
                if (item.Bname == name)
                {
                    this.skinComBookId.Text = item.BID + "";
                }
            }
        }
        #endregion
        #region 鼠标离开读者编号，读者名称组合事件
        //根据ID获取名字
        private void skinAuthor_MouseLeave(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.skinRID.Text.Trim()))
                return;

            int RID = int.Parse(this.skinRID.Text.Trim());
            foreach (var item in new BLL.ReaderInfoBLL().GetReaderInfo())
            {
                if (item.ReaderID == RID)
                {
                    this.skinTextReaderName.Text = item.ReaderName;
                }
            }
        }
        //根据名字获取ID
        private void skinTextReaderName_MouseLeave(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.skinTextReaderName.Text.Trim()))
                return;

            string skinTextReaderName = this.skinTextReaderName.Text.Trim();
            foreach (var item in new BLL.ReaderInfoBLL().GetReaderInfo())
            {
                if (item.ReaderName == skinTextReaderName)
                {
                    this.skinRID.Text = item.ReaderID + "";
                }
            }
        }
        #endregion

        //判断文本
        public bool CheckText()
        {
            if (String.IsNullOrEmpty(skinRID.Text.Trim()))
            {
                MessageBox.Show("请输入借阅证号", "系统提示");
                return false;
            }
            return true;
        }
        //图书借阅
        public void BorrowerBook()
        {
            DialogResult dr = MessageBox.Show("确定借阅《" + this.skinBname.Text.Trim() + "》么？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                borrowerIfno bi = new borrowerIfno()
                {
                    ReaderID = int.Parse(skinRID.Text.Trim()),
                    Bid = int.Parse(this.skinComBookId.Text),
                    BorrowerDate = skinDataTime.Value,
                    Borrowerremark = rtfRichMain.Text.Trim(),
                    BorrowerSate = skinComBorrwer.Text,
                };
                string sql = new BLL.borrowerIfnoBLL().InsertBookBorrwer(bi);
                MessageBox.Show(sql, "系统提示");
                this.Close();
            }
        }
        //归还图书
        public void ReturnBook()
        {
            DialogResult dr = MessageBox.Show("确定归还《" + this.skinBname.Text.Trim() + "》么？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                borrowerIfno bi = new borrowerIfno()
                {
                    BorrowerID = int.Parse(skinborrowerID.Text.Trim()),
                    Bid = int.Parse(this.skinComBookId.Text),
                    Borrowerreturn = dateTime2.Value,
                    Borrowerremark = rtfRichMain.Text.Trim(),
                    BorrowerreturnSate = skinComboReturn.Text,
                    BorrowerMakeup = skinComMakeup.Text,
                    BorrowerMake = String.IsNullOrEmpty(skinComboMakeUp2.Text) ? "已赔偿" : skinComboMakeUp2.Text
                };
                string sql = new BLL.borrowerIfnoBLL().updateBookBorrwer(bi);
                MessageBox.Show(sql, "系统提示");
                FB.GetInfo();
                this.Close();
            }
        }
        #endregion
        private void FrmBorrwer_Load(object sender, EventArgs e)
        {
            if (this.Text == "还书登记")
            {
                loadBorrower();
            }
            ComBoxLoad();
        }

        private void skinButtonOK_Click(object sender, EventArgs e)
        {
            if (this.Text == "还书登记")
                ReturnBook();
            else
                BorrowerBook();
        }

        private void skinButtonRest_Click(object sender, EventArgs e)
        {
            skinborrowerID.Text = "";
            skinBname.Text = "";
            rtfRichMain.Text = "";
            skinRID.Text = "";
            skinTextReaderName.Text = "";
        }

        private void skinButtonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
    }
}
